//
//  OldPosApplyViewController.m
//  NewPro
//
//  Created by RYX on 2018/11/1.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "OldPosApplyViewController.h"
#import "PosApplyStore.h"
#import "PosApplyList.h"
#import "PosApplyModel.h"
#import "PosApplyCollectionReusableView.h"
#import "PosChildModel.h"

#import "PosApplyCellOne.h"
#import "PosApplyCellTwo.h"
#import "PosApplyCellThree.h"

@interface OldPosApplyViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView *collectionView;
@property (copy, nonatomic) NSArray *listArr;

@end

@implementation OldPosApplyViewController


- (UICollectionView *)collectionView{
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = BASECOLOR;
        [_collectionView registerNib:[UINib nibWithNibName:@"PosApplyCellOne" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CELL1"];
        [_collectionView registerNib:[UINib nibWithNibName:@"PosApplyCellTwo" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CELL2"];
        [_collectionView registerNib:[UINib nibWithNibName:@"PosApplyCellThree" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CELL3"];
        [_collectionView registerClass:[PosApplyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        MJWeakSelf
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [weakSelf refresh];
        }];
        
        header.lastUpdatedTimeLabel.hidden = YES;
        _collectionView.mj_header = header;
    }
    
    return _collectionView;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.collectionView];

    [self.collectionView  mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(8);
        make.bottom.equalTo(self.view).offset(-45);
    }];
    
    [self.collectionView.mj_header beginRefreshing];
}


- (void)refresh{
    
    MJWeakSelf
    PosApplyStore *store = [[PosApplyStore alloc] init];
    [store getPosListArrWithTopId:@"1" Success:^(NSArray *listArr) {
        
        weakSelf.listArr = listArr;
        [weakSelf.collectionView reloadData];
        [weakSelf.collectionView.mj_header endRefreshing];
        
    } Failure:^(NSError *error) {
        
        [weakSelf.collectionView.mj_header endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}

- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.listArr.count;
    
}

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    PosChildModel *model = self.listArr[section];
    NSArray *arr = [PosApplyModel mj_objectArrayWithKeyValuesArray:model.posModels];
    return arr.count;
    
}


//生成cell  生成cell所用的类就是我们之前所自定义的类，所以自定义的ImageView以及label都可以用上
- (UICollectionViewCell *) collectionView:(UICollectionView *) collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    PosChildModel *model = self.listArr[indexPath.section];
    NSArray *arr = [PosApplyModel mj_objectArrayWithKeyValuesArray:model.posModels];
    PosApplyModel *cmodel = arr[indexPath.row];
    
    switch (cmodel.categoryId) {
        case 2:{
            
            PosApplyCellOne * cell = (PosApplyCellOne *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CELL1" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor whiteColor];
            cell.model = cmodel;
            return cell;
            break;
        }
        case 3:{
            
            PosApplyCellTwo * cell = (PosApplyCellTwo *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CELL2" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor whiteColor];
            cell.model = cmodel;
            return cell;
            break;
            
        }
        
        default:{
            
            PosApplyCellThree * cell = (PosApplyCellThree *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CELL3" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor whiteColor];
            cell.model = cmodel;
            
            return cell;
            break;
        }
            break;
    }
}


//设置每个item的尺寸 这个才是背景为黄色的item

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake((ScreenWidth - 16) / 2, 275);
    
}


//设置每个item的UIEdgInsets 相对于上左下右四个边界的位移

- (UIEdgeInsets) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(5, 5, 5, 5);
    
}


//设置每个item的水平间距

- (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 5;
    
}


//行间距 也就是所谓的水平间距

- (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 5;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PosChildModel *model = self.listArr[indexPath.section];
    NSArray *arr = [PosApplyModel mj_objectArrayWithKeyValuesArray:model.posModels];
    
    [PosApplyList sharedInstance].selectedPosModel = arr[indexPath.row];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(onCellWith:)]) {
        
        [self.delegate onCellWith:2];
    }
}

//设置头尾部内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    PosChildModel *model = self.listArr[indexPath.section];

    UICollectionReusableView *reusableView = nil;
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        //定制头部视图的内容
        PosApplyCollectionReusableView *headerV = (PosApplyCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        headerV.titleLa.text = model.title;
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:model.des attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Regular" size: 10],NSForegroundColorAttributeName: [UIColor colorWithRed:248/255.0 green:105/255.0 blue:18/255.0 alpha:1.0]}];
        headerV.desLa.attributedText = attStr;
        reusableView = headerV;
        
    }
    return reusableView;
    
}

//执行的 headerView 代理  返回 headerView 的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    PosChildModel *model = self.listArr[section];
    if ([model.title isEqualToString:@"购机循环送"]) {
        
        return CGSizeMake(ScreenWidth, 78);
    }
    return CGSizeMake(ScreenWidth, 30);
}



@end
